#!/usr/bin/perl

######################################################################
package Swignition::uF::hResume;
######################################################################

use CGI::Util;
use Swignition::GenericParser::Utils;
use Swignition::uF;
use XML::LibXML qw(:all);
use strict;


sub consume
{
	my $page        = shift;

	unless ( $page->{Profiles}->{'http://microformats.org/wiki/hresume-profile'}
	||      !$page->{options}->{uf_strict_profiles})
	{
		return;
	}
	
	$page->{uF}->{hResume} = [ parse_all($page) ];
	foreach my $r (@{ $page->{uF}->{hResume} })
		{ $r->rdf_subject_merge($page); }
	if (defined $page->{uF}->{hResume}->[0])
		{ $page->mergeNS($page->{uF}->{hResume}->[0]); }
}


sub parse_all
{
	my $page   = shift;
	my $within = shift || $page->{DOM};
	my @rv;
	
	my @nodes = searchClass('hresume', $within);
	foreach my $a (@nodes)
	{
		my $A = parse($page, $a);
		push @rv, $A;
	}
	
	return @rv;
} #/sub parse_all


sub uri
{
	if (defined $_[0]->{contact})
		{ return $_[0]->{contact}->uri($_[1]); }
		
	my $this = shift;
	my $all  = shift;
	my @rv;
		
	if (length $this->{_id})
	{
		push @rv, $this->{_page}->uri.'#'.$this->{_id};
	}
	if (lc($this->{_dom}->tagName) eq 'body')
	{
		push @rv, $this->{_page}->uri;
	}
	push @rv, Swignition::GenericParser::Utils::BNodeURI($this->{_dom}->getAttribute('_xpath'), 'Contact')
		unless (@rv);

	unless (defined $all) { return $rv[0];}
	while ($all) { shift @rv; $all--; }
	return @rv;

} #/sub uri


sub rdf_subject_merge
{
	my $this    = shift;
	my $page    = shift;
	my $val;

	my $subject = Swignition::RDFModel::Subject->new($this->uri);
	$subject->addObject($this);
	$subject->setDom($this->{_dom});
	
	$subject->addVal('http://purl.org/dc/terms/abstract',
		Swignition::RDFModel::Value->new($this->{summary}))
		if (length $this->{summary});
		
	foreach my $prop (qw(education experience))
	{
		foreach my $x (@{$this->{$prop}})
		{
			my $X = Swignition::RDFModel::Subject->new($x->uri);
			$X->addVal('http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
				Swignition::RDFModel::Value->new('http://ramonantonio.net/doac/0.1/'.ucfirst($prop), 1));
			$X->addVal('http://ramonantonio.net/doac/0.1/date-starts',
				Swignition::RDFModel::Value->new($x->{dtstart}))
				if ($x->{dtstart});
			$X->addVal('http://ramonantonio.net/doac/0.1/date-ends',
				Swignition::RDFModel::Value->new($x->{dtend}))
				if ($x->{dtend});
			$X->addVal('http://ramonantonio.net/doac/0.1/'.($prop eq 'education' ? 'subject' : 'activity'),
				Swignition::RDFModel::Value->new($x->{description}))
				if ($x->{description});
			foreach my $l (@{$x->{location}})
			{
				if (UNIVERSAL::can($l, 'uri'))
				{
					$X->addVal('http://ramonantonio.net/doac/0.1/location',
						Swignition::RDFModel::Value->new($l->uri, 1));
				}
				else
				{
					$X->addVal('http://ramonantonio.net/doac/0.1/location',
						Swignition::RDFModel::Value->new($l));
				}
			}
			if ($x->{associated_hcard})
			{
				foreach my $o (@{$x->{associated_hcard}->{org}})
				{
					$X->addVal('http://ramonantonio.net/doac/0.1/organization',
						Swignition::RDFModel::Value->new($o->uri, 1))
				}
				foreach my $t (@{$x->{associated_hcard}->{title}})
				{
					$X->addVal('http://ramonantonio.net/doac/0.1/'.($prop eq 'education' ? 'title' : 'position'),
						Swignition::RDFModel::Value->new($t));
				}
				$subject->addVal('http://purl.org/uF/hCard/terms/hasHistoricCard',
					Swignition::RDFModel::Value->new($x->{associated_hcard}->uri, 1))
					if ($prop eq 'experience');
			}
			
			$page->mergeSubject($X);
			$subject->addVal('http://ramonantonio.net/doac/0.1/'.$prop,
				Swignition::RDFModel::Value->new($x->uri, 1));
		}
	}
	
	foreach my $x (@{$this->{skill}})
	{
		$subject->addVal('http://ramonantonio.net/doac/0.1/skill',
			Swignition::RDFModel::Value->new($x));
	}
	
	foreach my $x (@{$this->{contact}->{lang}})
	{
		my $Xuri = Swignition::GenericParser::Utils::BNodeURI(undef, 'LanguageSkill');
		my $X = Swignition::RDFModel::Subject->new($Xuri);
		$X->addVal('http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
			Swignition::RDFModel::Value->new('http://ramonantonio.net/doac/0.1/LanguageSkill', 1));
		$X->addVal('http://ramonantonio.net/doac/0.1/language',
			Swignition::RDFModel::Value->new($x));

		$page->mergeSubject($X);
		$subject->addVal('http://ramonantonio.net/doac/0.1/skill',
			Swignition::RDFModel::Value->new($Xuri, 1));
	}

	foreach my $x (@{$this->{publication}})
	{
		$subject->addVal('http://ramonantonio.net/doac/0.1/publication',
			Swignition::RDFModel::Value->new($x));
	}
	
	foreach my $x (@{$this->{affiliation}})
	{
		my $X = Swignition::RDFModel::Subject->new($x->uri);
		$X->addVal('http://buzzword.org.uk/rdf/vcardx#member',
			Swignition::RDFModel::Value->new($this->{contact}->uri, 1));

		$page->mergeSubject($X);
	}

	$page->mergeSubject($subject);
	
} #/sub rdf_subject


sub metadata_ns
{
	my $this = shift;
	return {
		'doac' => {
				nsurl=>'http://ramonantonio.net/doac/0.1/',
				title=>'Description of a Career'
			}
		};
} #/sub metadata_ns


sub parse
{
	my $page  = shift;
	my $rv    = { '_dom'=>shift };
	my $pkg = __PACKAGE__;
	if (defined $page->{uF}->{_Shortcuts}->{$pkg}->{ $rv->{_dom}->getAttribute('_xpath') })
		{ return $page->{uF}->{_Shortcuts}->{$pkg}->{ $rv->{_dom}->getAttribute('_xpath') }; }
	else
		{ $page->{uF}->{_Shortcuts}->{$pkg}->{ $rv->{_dom}->getAttribute('_xpath') } = $rv; }
	
	my $root  = $rv->{'_dom'}->cloneNode(1);
	my $root2 = $rv->{'_dom'}->cloneNode(1);
	
	my $id = $root->getAttribute('id');
	$rv->{_id} = $id if (length $id);
	$rv->{_page} = $page;
	
	data_patterns($page, $root,  2);
	data_patterns($page, $root2, 2);
	
	simple_parse(
		$rv,
		$root,
		[
			['summary',     '?'],
			['contact',     'm?',  {'embedded'=>'hCard'}],
			['address',     'tm?', {'embedded'=>'hCard'}],
			['education',   'm*',  {'embedded'=>'hEvent', 'allow-interleaved' => ['vcalendar']}],
			['experience',  'm*',  {'embedded'=>'hEvent', 'allow-interleaved' => ['vcalendar']}],
			['skill',       '*'],
			['affiliation', 'M*',  {'embedded'=>'hCard'}],
			['cite',        't',   {'use-key'=>'publication'}]
		]
	);
	
	$rv->{'contact'} = $rv->{'address'}
		unless (defined $rv->{'contact'});
	
	simple_parse(
		$rv,
		$root2,
		[
			['experience',  'm*', {'embedded'=>'hCard','use-key'=>'experience-hcard','allow-interleaved' => ['vcalendar', 'vevent']}],
			['education',   'm*', {'embedded'=>'hCard','use-key'=>'education-hcard', 'allow-interleaved' => ['vcalendar', 'vevent']}]
		]
	);
	
	foreach my $prop (qw(education experience))
	{
		foreach my $e ( @{$rv->{$prop}} )
		{
			foreach my $ehc ( @{$rv->{$prop.'-hcard'}} )
			{
				my $ehcxp = $ehc->{parent_property_node}->getAttribute('_xpath');
				if ($ehcxp eq $e->{'_dom'}->getAttribute('_xpath'))
				{
					$e   -> {'associated_hcard'}   = $ehc;
					$ehc -> {'associated_hevent'}  = $e;
				}
			}
		}
	}
	
	bless $rv;
	return $rv;
	
} #/sub parse


1; #/package Swignition::uF::hResume

